//
//  StatusPictureView.swift
//  SynWeibo
//
//  Created by 沈艳男 on 15/9/12.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

import UIKit
import SDWebImage
// 定义可重用标识符
private let StatusPictureViewCellID = "StatusPictureViewCellID"

class StatusPictureView: UICollectionView {
    
    
    var statusViewModel: StatusViewModel? {
        didSet {
            // 自动设置
            sizeToFit()
            // 刷新数据
            reloadData()
        }
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
        return calcViewSize()
    }
    
    // 根据模型中的数量来计算图片的大小
    private func calcViewSize() -> CGSize {
        
        // 1、获取布局大小
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置默认图片的大小
        layout.itemSize = CGSize(width: StatusPictureItemWidth, height: StatusPictureItemWidth)
        
        // 2、取出模型内部图片的数量，根据图片的数量来计算整个试图的大小
        let count = statusViewModel?.thumbnailURLs?.count ?? 0
        
        // 计算图片的行数
        // 1、如果是0长图片
        if count == 0 {
            return CGSizeZero
        }
        // 2、如果是1张图
        if count == 1 {
            let size = CGSize(width: 150, height: 150)
            layout.itemSize = size
            return size
        }
        // 3、如果是4张图
        if count == 4 {
            let w = 2 * StatusPictureItemWidth + StatusPictureItemMargin
            
            return CGSize(width: w, height: w)
        }
        // 4、如果是其他数量
        /**
        2, 3,
        5, 6,
        7, 8, 9
        */
        // 计算显示的图片的行数
        let row = CGFloat((count - 1) / Int(StatusPictureMaxCount) + 1)
        let h = row * StatusPictureItemWidth + (row - 1) * StatusCellMargin
        let w = StatusPictureMaxWidth

        
        return CGSize(width: w, height: h)
    }
//
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
//        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        backgroundColor = UIColor.whiteColor()
        
        // 设置布局之间的间距
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = StatusPictureItemMargin
        layout.minimumLineSpacing = StatusPictureItemMargin
        
        // 制定数据源，让自己成为自己的数据源
        dataSource = self
        // 注册科重用 cell
        registerClass(StatusPictureViewCell.self, forCellWithReuseIdentifier: StatusPictureViewCellID)
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


extension StatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusViewModel?.thumbnailURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureViewCellID, forIndexPath: indexPath) as! StatusPictureViewCell
        cell.imageURL = statusViewModel!.thumbnailURLs! [indexPath.item]
        
        return cell
    }
}

// 设置cell
private class StatusPictureViewCell: UICollectionViewCell {
    
    
    // 配图试图的URL，是用SDWimage下载图片
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    // MARK: - 构造函数 
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        // 设置布局，将该图片填满整个试图
        iconView.ff_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 懒加载控件
    private let iconView: UIImageView = UIImageView()
    
}
